﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NBusiness.CodeDom;
using NBusiness.ESharp.Compiler;
using NBusiness.CodeDom.Compiler;

namespace NBusiness.ESharp
{
    [Serializable]
    class ESharpValidationArgument : EntityValidationArgument
    {
        protected override void Attach()
        {
            foreach (EntityElement element in Elements)
            {
                if (element.Type == ESharpEntityElementTypes.ValidateArgName)
                {
                    ESharpValidation rule = Parent as ESharpValidation;
                    if (!rule.Matched.Arguments.Contains(this))
                        OnBuildError(new ESharpBuildError(ESharpErrorCode.ES5105, element, EntityBuildErrorType.Warning, Name));
                }
            }
        }

        protected override void InitializeAssignmentLogic()
        {
            Assignments.Add(new AssignmentLogic(ESharpEntityElementTypes.ValidateArgAssign));
            Assignments.Add(new AssignmentLogic(
                ESharpEntityElementTypes.ValidateArgValue,
                AssignValue,
                PostAssignmentAction.Stop));

            Assignments.Add(new AssignmentLogic(ESharpEntityElementTypes.WhiteSpace));
            Assignments.Add(new AssignmentLogic(ESharpEntityElementTypes.Comment));
        }

        private void AssignValue(object sender, AssignmentLogicEventArgs e)
        {
            Value = e.Elements.Peek().Value.Trim('"');
        }
    }
}
